/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class SimpleGraph implements Graph, Serializable {
	private Map nodeMap;

	public SimpleGraph() {
		this.nodeMap = new HashMap();
	}

	public Number addEdge(Object node1, Number weight, Object node2) {
		
		System.out.println("SimpleGraph.addEdge() n1: "+node1+" weight: "+weight+" n2: "+node2);
		
		if (node1 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		if (node2 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}

		Node a = (Node) nodeMap.get(node1);
		Node b = (Node) nodeMap.get(node2);
		if (a == null) {
			nodeMap.put(node1, a = new Node());
		}
		if (b == null) {
			nodeMap.put(node2, b = new Node());
		}
		return a.addEdge(weight, b);
	}

	public Number removeEdge(Object node1, Object node2) {
		if (node1 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		if (node2 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}

		Node a = (Node) nodeMap.get(node1);
		Node b = (Node) nodeMap.get(node2);
		if (a == null || b == null) {
			return null;
		} else {
			return a.removeEdge(b);
		}
	}

	public Number getWeigth(Object node1, Object node2) {
		if (node1 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		if (node2 == null) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}

		Node a = (Node) nodeMap.get(node1);
		Node b = (Node) nodeMap.get(node2);
		if (a == null || b == null) {
			return null;
		} else {
			return a.getWeight(b);
		}
	}

	public Set getNodes() {
		return nodeMap.keySet();
	}

	private class Node {
		private Map edges;

		public Node() {
			edges = new HashMap();
		}

		public Number addEdge(Number weight, Node node) {
			Object result = edges.put(node, weight);
			return (result == null) ? null : (Number) result;
		}

		public Number removeEdge(Node node) {
			return (Number) edges.remove(node);
		}

		public Number getWeight(Node node) {
			return (Number) edges.get(node);
		}

		public String toString() {
			StringBuffer buffer = new StringBuffer();
			Set nodesDescription = new TreeSet(nodeMap.keySet());
			Iterator it = nodesDescription.iterator();
			if (it.hasNext()) {
				Object node = nodeMap.get(it.next());
				while (!edges.containsKey(node) && it.hasNext()) {
					node = nodeMap.get(it.next());
				}
				if (edges.containsKey(node)) {
					buffer.append(edges.get(node));
				}
			}
			for (; it.hasNext(); ) {
				Object node = nodeMap.get(it.next());
				if (edges.containsKey(node)) {
					buffer.append(" " + edges.get(node));
				}
			}
			return buffer.toString();
		}

	}

	public String toString() {
		
		System.out.println("SimpleGraph.toString() "+nodeMap.keySet());
		
		StringBuffer buffer = new StringBuffer();
		Set nodesDescription = new TreeSet(nodeMap.keySet());
		System.out.println("SimpleGraph.toString() "+nodesDescription);
		Iterator it = nodesDescription.iterator();
		if (it.hasNext()) {
			buffer.append(nodeMap.get(it.next()));
		}
		for (; it.hasNext(); ) {
			buffer.append(System.getProperty("line.separator") + nodeMap.get(it.next()));
		}
		return buffer.toString();
	}

}
